package com.example.tools;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.example.administrator.olddoctor.R;

/**
 * Created by Administrator on 2016/9/26.
 */
public class MyProgressBar {
    Context context = null;
    MyProgressDialog pBar = null;

    public MyProgressBar(Context c) {
        context = c;
    }

    public boolean isShowing() {
        return (pBar == null) ? false : pBar.isShowing();
    }

    public void show(final Runnable r, boolean pShow) {
        if (pBar != null && pBar.isShowing()) {
            pBar.dismiss();
        }
        pBar = new MyProgressDialog(context);
        pBar.setMax(100);
        pBar.setProgress(0);
        pBar.setIndeterminate(true);
        pBar.setCancelable(true);
        pBar.setCanceledOnTouchOutside(false);
        if (pShow) {
            try {
                pBar.show();
            } catch (Exception E) {
                E.printStackTrace();
            }
        }
        if (r != null) {
            new Thread(r).start();
            new Handler().postDelayed(new Runnable() {

                public void run() {
                    try {
                        pBar.dismiss();
                    } catch (Exception E) {
                    }
                }
            }, 1000 * 60 * 2);
        }
    }

    public void show() {
        show(null, true);
    }

    public void show(Runnable r) {
        show(r, true);
    }

    public void dismiss() {
        if (null != pBar && pBar.isShowing()) {
            try {
                pBar.dismiss();
            } catch (Exception e) {

            }
        }

    }

    public void setOnDismissListener(OnDismissListener listener) {
        pBar.setOnDismissListener(listener);
    }

    public void stopThread() {
        if (pBar != null && pBar.isShowing()) {
        }
    }

    class MyProgressDialog extends Dialog implements DialogInterface {
        public static final int STYLE_SPINNER = 0;
        public static final int STYLE_HORIZONTAL = 1;

        private android.widget.ProgressBar mProgress;

        private int mProgressStyle = STYLE_SPINNER;
        private int mMax;
        private int mProgressVal;
        private int mSecondaryProgressVal;
        private int mIncrementBy;
        private int mIncrementSecondaryBy;
        private Drawable mProgressDrawable;
        private Drawable mIndeterminateDrawable;
        private boolean mIndeterminate;
        private boolean mHasStarted;
        private Handler mViewUpdateHandler;
        Context mContext;

        public MyProgressDialog(Context context) {
            super(context, R.style.dialog);

            mContext = context;
        }

        public void setView(View view) {
            Window mWindow = getWindow();
            mWindow.requestFeature(Window.FEATURE_NO_TITLE);
            mWindow.setContentView(view);
            // super.setView(view, 0, 0, 0, 0);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            {
                View view = inflater.inflate(R.layout.dialog_progress, null);
                mProgress = (android.widget.ProgressBar) view.findViewById(R.id.progress);
                setView(view);
            }
            if (mMax > 0) {
                setMax(mMax);
            }
            if (mProgressVal > 0) {
                setProgress(mProgressVal);
            }
            if (mSecondaryProgressVal > 0) {
                setSecondaryProgress(mSecondaryProgressVal);
            }
            if (mIncrementBy > 0) {
                incrementProgressBy(mIncrementBy);
            }
            if (mIncrementSecondaryBy > 0) {
                incrementSecondaryProgressBy(mIncrementSecondaryBy);
            }
            if (mProgressDrawable != null) {
                setProgressDrawable(mProgressDrawable);
            }
            if (mIndeterminateDrawable != null) {
                setIndeterminateDrawable(mIndeterminateDrawable);
            }
            setIndeterminate(mIndeterminate);
            onProgressChanged();

            super.onCreate(savedInstanceState);
        }

        @Override
        public void onStart() {
            super.onStart();
            mHasStarted = true;
        }

        @Override
        protected void onStop() {
            super.onStop();
            mHasStarted = false;
        }

        public void setProgress(int value) {
            if (mHasStarted) {
                mProgress.setProgress(value);
                onProgressChanged();
            } else {
                mProgressVal = value;
            }
        }

        public void setSecondaryProgress(int secondaryProgress) {
            if (mProgress != null) {
                mProgress.setSecondaryProgress(secondaryProgress);
                onProgressChanged();
            } else {
                mSecondaryProgressVal = secondaryProgress;
            }
        }

        public int getProgress() {
            if (mProgress != null) {
                return mProgress.getProgress();
            }
            return mProgressVal;
        }

        public int getSecondaryProgress() {
            if (mProgress != null) {
                return mProgress.getSecondaryProgress();
            }
            return mSecondaryProgressVal;
        }

        public int getMax() {
            if (mProgress != null) {
                return mProgress.getMax();
            }
            return mMax;
        }

        public void setMax(int max) {
            if (mProgress != null) {
                mProgress.setMax(max);
                onProgressChanged();
            } else {
                mMax = max;
            }
        }

        public void incrementProgressBy(int diff) {
            if (mProgress != null) {
                mProgress.incrementProgressBy(diff);
                onProgressChanged();
            } else {
                mIncrementBy += diff;
            }
        }

        public void incrementSecondaryProgressBy(int diff) {
            if (mProgress != null) {
                mProgress.incrementSecondaryProgressBy(diff);
                onProgressChanged();
            } else {
                mIncrementSecondaryBy += diff;
            }
        }

        public void setProgressDrawable(Drawable d) {
            if (mProgress != null) {
                mProgress.setProgressDrawable(d);
            } else {
                mProgressDrawable = d;
            }
        }

        public void setIndeterminateDrawable(Drawable d) {
            if (mProgress != null) {
                mProgress.setIndeterminateDrawable(d);
            } else {
                mIndeterminateDrawable = d;
            }
        }

        public void setIndeterminate(boolean indeterminate) {
            if (mHasStarted && (isIndeterminate() != indeterminate)) {
                mProgress.setIndeterminate(indeterminate);
            } else {
                mIndeterminate = indeterminate;
            }
        }

        public boolean isIndeterminate() {
            if (mProgress != null) {
                return mProgress.isIndeterminate();
            }
            return mIndeterminate;
        }

        private void onProgressChanged() {
            if (mProgressStyle == STYLE_HORIZONTAL) {
                mViewUpdateHandler.sendEmptyMessage(0);
            }
        }
    }
}
